﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Toulr.Services;
using Toulr.Util.Ioc;
using Toulr.Models;

public partial class Account_reg1 : CommPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            //服务器端校验
            txtEmail.Value = txtEmail.Value.Trim();

            if (!PageHelper.CheckEmail(txtEmail.Value))
            {
                AddClientErrorMessageBox("错误提示", "您的邮箱地址不正确,请修复！");
                return;
            }
            if (!CheckEmailExists(txtEmail.Value))
            {
                AddClientErrorMessageBox("错误提示", txtEmail.Value + "已经被注册,请输入您的Email地址！<br>如果该邮箱地址确实是您自己的，请使用点击[ <a href='#'>忘记密码</a> ]找回密码！");
                return;
            }

            if (!PageHelper.IsCorrectVerifyCode(txtVerifyCode.Value))
            {
                AddClientErrorMessageBox("错误提示", "校验码错误,请重新输入！");
                txtVerifyCode.Value = string.Empty;
                return;
            }
            
            //清除这个注册码
            PageHelper.ClearNewlyVerifyCode();

            IUserContextService iUser = UnityHelper.GetInstance<IUserContextService>();
            ActionResult<bool> actionResult = iUser.SendVerifyMail(txtEmail.Value,PageHelper.GetBaseUrl());
            if (actionResult.Result)
            {
                Server.Transfer("EmailSended.aspx?email=" + Uri.EscapeDataString(txtEmail.Value));
            }
        }
    }

    /// <summary>
    /// Checks the email.
    /// </summary>
    /// <param name="email">The email.</param>
    /// <returns></returns>
    /// <Author>Tecky Lee</Author>
    /// <Date>2011/5/14 20:49</Date>
    [AjaxPro.AjaxMethod]
    public bool CheckEmailExists(string email)
    {
        IUserContextService iUser = UnityHelper.GetInstance<IUserContextService>();
        return !(iUser.EmailRegistered(email).Result);
    }
}